﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Common.Objects;
using MyVoteMVC.Data.Repositories;


namespace MyVoteMVC.Service
{
    public class VoteDictionariesService : IDictionariesService
    {
        IUnitOfWork unitOfWork;
        IRepository<VT_Dictionaries> dictionariesRepository;
        IRepository<VT_Dictionaries> adminDictionariesRepository;

        public VoteDictionariesService()
        {
            unitOfWork = Factory.GetInstance<IUnitOfWork>();
            dictionariesRepository = unitOfWork.GetRepository<VT_Dictionaries>();
            adminDictionariesRepository = unitOfWork.GetAdminRepository<VT_Dictionaries>();
        }

        //增加
        public bool AddDictionary(string dName, string dtype, string dValue, int dOrder)
        {
            VT_Dictionaries v_Dictionaries = dictionariesRepository.GetMany(o => o.Dictionary_Name == dName).FirstOrDefault();
            if (v_Dictionaries!=null)
                throw new ArgumentException("已经存在相关记录");
            v_Dictionaries = new VT_Dictionaries();
            unitOfWork.BeginTransaction();
            v_Dictionaries.Dictionary_ID = Guid.NewGuid().ToString();
            v_Dictionaries.Dictionary_Name = dName;
            v_Dictionaries.Dictionary_Type = dtype;
            v_Dictionaries.Dictionary_Value = dValue;
            v_Dictionaries.Dictionary_Order = dOrder;
            adminDictionariesRepository.Add(v_Dictionaries);
            unitOfWork.CommitTransaction();
            return true;
        }
        //修改
        public bool ModifyDictionary(string dID, string dName, string dtype, string dValue, int dOrder)
        {
            VT_Dictionaries v_Dictionaries = dictionariesRepository.GetMany(o => o.Dictionary_Name == dName).FirstOrDefault();
            //if (v_Dictionaries != null)
            //    throw new ArgumentException("已经存在相关记录");
            v_Dictionaries = dictionariesRepository.GetMany(o => o.Dictionary_ID == dID).FirstOrDefault();
            if (v_Dictionaries != null)
            {
                unitOfWork.BeginTransaction();
                v_Dictionaries.Dictionary_Name = dName;
                v_Dictionaries.Dictionary_Type = dtype;
                v_Dictionaries.Dictionary_Value = dValue;
                v_Dictionaries.Dictionary_Order = dOrder;
                adminDictionariesRepository.Update(v_Dictionaries);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
        //删除
        public  bool DeleteDictionary(string dID)
        {
            VT_Dictionaries v_Dictionaries = dictionariesRepository.GetMany(o => o.Dictionary_ID == dID).FirstOrDefault();
            if (v_Dictionaries != null)
            {
                unitOfWork.BeginTransaction();
                adminDictionariesRepository.Delete(v_Dictionaries);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
        //获取
        public IList<VT_Dictionaries> GetAll()
        {
           IList<VT_Dictionaries> v_Dictionaries = dictionariesRepository.GetAll().OrderBy(o=>o.Dictionary_Type).ToList();
            return v_Dictionaries;
        }
        //获取单个
        public VT_Dictionaries GetByID(string dID)
        {
            VT_Dictionaries v_Dictionaries = dictionariesRepository.GetMany(o => o.Dictionary_ID == dID).FirstOrDefault();
            return v_Dictionaries;
        }

        public VT_Dictionaries GetByName(string dName)
        {
            VT_Dictionaries v_Dictionaries = dictionariesRepository.GetMany(o => o.Dictionary_Name == dName).FirstOrDefault();
            return v_Dictionaries;
        }

        #region IDisposable Members

        private bool m_disposed;
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    unitOfWork.Dispose();   
                }

                // Release unmanaged resources

                m_disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~VoteDictionariesService()
        {
            Dispose(false);
        }
        #endregion
    }
}
